import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;



public class NodeSpellbook {
	Player player;
	ImageIcon image = new ImageIcon(getClass().getResource("/Pics/bag.png"));
	ArrayList<AttackName> listWithAttacks = new ArrayList<AttackName>();
	protected int currentInventoryLevel = 0;
	protected int finalInventoryLevel = 0;
	String[][] inventoryLevels = new String[20][6];
	final int ENLARGE_VALUE = 100;
	InterfaceFrame frame;
	JComponent lastButton;
	CustomButton item1 = new CustomButton();
	CustomButton item2 = new CustomButton();
	CustomButton item3 = new CustomButton();
	CustomButton item4 = new CustomButton();
	CustomButton item5 = new CustomButton();
	CustomButton item6 = new CustomButton();
	CustomButton back1 = new CustomButton();
	CustomButton back2 = new CustomButton();

	
	AttackName currentAttack;
	AttackName removeAttack;
	
	CustomButton attack[] = new CustomButton[4];
	
	CustomButton activePet = new CustomButton();
	CustomButton passivePet = new CustomButton();
	CustomArrowButton up = new CustomArrowButton("Up");
	CustomArrowButton down = new CustomArrowButton("Down");
	
	ImageIcon nature = new ImageIcon(getClass().getResource("/Pics/natureIcon.gif"));
	ImageIcon fire = new ImageIcon(getClass().getResource("/Pics/fireIcon.gif"));
	ImageIcon water = new ImageIcon(getClass().getResource("/Pics/waterIcon.gif"));
	ImageIcon energy = new ImageIcon(getClass().getResource("/Pics/energyIcon.gif"));
	
	
	NodeSpellbook(Player player, InterfaceFrame frame){
		listWithAttacks = player.spellBook.getAttacks();
		//player.spellBook.addItem(AttackName.EldIllussion);
		this.player = player;
		this.frame = frame;
		createInventoryLevels();
		
		up.addActionListener(new ItemListener());
		down.addActionListener(new ItemListener());
		item1.addActionListener(new ItemListener());
		item2.addActionListener(new ItemListener());
		item3.addActionListener(new ItemListener());
		item4.addActionListener(new ItemListener());
		item5.addActionListener(new ItemListener());
		item6.addActionListener(new ItemListener());
		activePet.addActionListener(new ItemListener());
		passivePet.addActionListener(new ItemListener());
		back1.addActionListener(new ItemListener());
		back2.addActionListener(new ItemListener());
		
		for(int i=0; i<4; i++){
			attack[i]=new CustomButton();
			attack[i].addActionListener(new ItemListener());
			attack[i].addMouseListener(new TheMouseListener());
		}
		
		item1.addMouseListener(new TheMouseListener());
		item2.addMouseListener(new TheMouseListener());
		item3.addMouseListener(new TheMouseListener());
		item4.addMouseListener(new TheMouseListener());
		item5.addMouseListener(new TheMouseListener());
		item6.addMouseListener(new TheMouseListener());
		back1.addMouseListener(new TheMouseListener());
		back2.addMouseListener(new TheMouseListener());
		up.addMouseListener(new TheMouseListener());
		down.addMouseListener(new TheMouseListener());
		activePet.addMouseListener(new TheMouseListener());
		passivePet.addMouseListener(new TheMouseListener());
		
	}
	
	public ImageIcon getPic(){
		return image;
	}
	
	private ImageIcon getTypeIcon(int index){
		if(new Attack(listWithAttacks.get(currentInventoryLevel*6 + index)).getType().equals(Type.Energy))
			return energy;
		else if(new Attack(listWithAttacks.get(currentInventoryLevel*6 + index)).getType().equals(Type.Water))
			return water;
		else if(new Attack(listWithAttacks.get(currentInventoryLevel*6 + index)).getType().equals(Type.Fire))
			return fire;
		else
			return nature;
	}
	
	public void showInventoryLevel(int level){
		
		createInventoryLevels();
		frame.nface.popOptions.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
		
		if(inventoryLevels[level][0] != null){
			item1.setIcon(getTypeIcon(0));
			item1.setText(inventoryLevels[level][0]);
		}
		if(inventoryLevels[level][1] != null){
			item2.setIcon(getTypeIcon(1));
			item2.setText(inventoryLevels[level][1]);
		}
		if(inventoryLevels[level][2] != null){
			item3.setIcon(getTypeIcon(2));
			item3.setText(inventoryLevels[level][2]);
		}
		if(inventoryLevels[level][3] != null){
			item4.setIcon(getTypeIcon(3));
			item4.setText(inventoryLevels[level][3]);
		}
		if(inventoryLevels[level][4] != null){
			item5.setIcon(getTypeIcon(4));
			item5.setText(inventoryLevels[level][4]);
		}
		if(inventoryLevels[level][5] != null){
			item6.setIcon(getTypeIcon(5));
			item6.setText(inventoryLevels[level][5]);
		}
		
		if(inventoryLevels[level][0] != null)
			frame.nface.popOptions.add(item1);
		else
			frame.nface.popOptions.remove(item1);
		if(inventoryLevels[level][1] != null)
			frame.nface.popOptions.add(item2);
		else
			frame.nface.popOptions.remove(item2);
		if(inventoryLevels[level][2] != null)
			frame.nface.popOptions.add(item3);
		else
			frame.nface.popOptions.remove(item3);
		if(inventoryLevels[level][3] != null)
			frame.nface.popOptions.add(item4);
		else
			frame.nface.popOptions.remove(item4);
		if(inventoryLevels[level][4] != null)
			frame.nface.popOptions.add(item5);
		else
			frame.nface.popOptions.remove(item5);
		if(inventoryLevels[level][5] != null)
			frame.nface.popOptions.add(item6);
		else
			frame.nface.popOptions.remove(item6);
		
		if(currentInventoryLevel != finalInventoryLevel && listWithAttacks.size() > 6)
			frame.nface.popOptions.add(down);
		else
			frame.nface.popOptions.remove(down);
		if(currentInventoryLevel != 0)
			frame.nface.popOptions.add(up);
		else
			frame.nface.popOptions.remove(up);
		
		frame.nface.textArea.setText("Välj attack att lägga till ett husdjur.");

	}
	
	//Skapar en större array, behövs då vi inte vet hur många items spelaren har.
    private void enlarge(){
    	
    	int newLength = 1 + inventoryLevels.length + ENLARGE_VALUE * inventoryLevels.length/100;
    	String[][] newArray = new String[newLength][6];
    	
    	for(int index = 0; index < inventoryLevels.length; index++){
    		for(int j = 0; j < 6; j++)
    			newArray[index][j] = inventoryLevels[index][j];
    	}
    	
    	inventoryLevels = newArray;
    	
    }
    
    public void createInventoryLevels(){
    	
    	inventoryLevels = new String[20][6];
    	listWithAttacks = player.spellBook.getAttacks();
    	
    	//Undersöker vilken sista Leveln är.
    	if(listWithAttacks.size()%6 == 0)
    		finalInventoryLevel = listWithAttacks.size()/6 - 1;
    	else
    		finalInventoryLevel = listWithAttacks.size()/6;
    	
    	String level[] = new String[6];
    	int index = 0;
    	
    	for(int i = 0; i <= finalInventoryLevel; i++){
    		
    		//Om inventoryLevels arrayen är full, skapa en större!
    		if(inventoryLevels.length <= index)
    			enlarge();
    		
    		//Lopar fram en sträng till varje index som innehåller hur många items och namnet.
    		for(int j = 0; j < 6; j++){
    			
    			level[i] = new Attack(listWithAttacks.get(index)).getName();
    			inventoryLevels[i][j] = level[i];
    			index++;
    			
    			//Ifall vi kommit till slutet bryt.
    			if(index >= listWithAttacks.size()){
    				break;
    			}
    			
    		}
    		
    	}
    	
    }
    
    private void showPets(){
    	if(item1 != null)
			frame.nface.popOptions.remove(item1);
		if(item2 != null)
			frame.nface.popOptions.remove(item2);
		if(item3 != null)
			frame.nface.popOptions.remove(item3);
		if(item4 != null)
			frame.nface.popOptions.remove(item4);
		if(item5 != null)
			frame.nface.popOptions.remove(item5);
		if(item6 != null)
			frame.nface.popOptions.remove(item6);
		if(down != null)
			frame.nface.popOptions.remove(down);
		if(up != null)
			frame.nface.popOptions.remove(up);
		
		activePet.setText(player.activePet.name);
		frame.nface.popOptions.add(activePet);
		if(player.passivePet!=null){
			passivePet.setText(player.passivePet.name);
			frame.nface.popOptions.add(passivePet);
		}
		back1.setText("Tillbaks");
		frame.nface.popOptions.add(back1);
		frame.getContentPane().repaint();
    }
    
    public void showPetAttacks(){
    	if(activePet != null)
			frame.nface.popOptions.remove(activePet);
		if(passivePet != null)
			frame.nface.popOptions.remove(passivePet);
		if(back1 != null)
			frame.nface.popOptions.remove(back1);
		if(lastButton==activePet){
			for(int i=0; i<player.activePet.attacks.size(); i++){
				attack[i].setText(player.activePet.attacks.get(i).name());
				frame.nface.popOptions.add(attack[i]);
			}
			if(player.activePet.attacks.size()<4){
				for(int i=player.activePet.attacks.size(); i<4; i++){
					attack[i].setText("Lägg till ny attack");
					frame.nface.popOptions.add(attack[i]);
				}
			}
		}
		if(lastButton==passivePet){
			for(int i=0; i<player.passivePet.attacks.size(); i++){
				attack[i].setText(player.passivePet.attacks.get(i).name());
				frame.nface.popOptions.add(attack[i]);
			}
			if(player.passivePet.attacks.size()<4){
				for(int i=player.passivePet.attacks.size(); i<4; i++){
					attack[i].setText("Lägg till ny attack");
					frame.nface.popOptions.add(attack[i]);
				}
			}
		}
		back2.setText("Tillbaks");
		frame.nface.popOptions.add(back2);
		frame.getContentPane().repaint();
		
    }
	
	private class ItemListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource().equals(down)){

				if(lastButton != null)
					lastButton.setForeground(Color.BLACK);
				lastButton = down;
				down.setForeground(Color.BLUE);
				currentInventoryLevel++;
				showInventoryLevel(currentInventoryLevel);
			}
			else if(e.getSource().equals(up)){

				if(lastButton != null)
					lastButton.setForeground(Color.BLACK);
				lastButton = up;
				up.setForeground(Color.BLUE);
				currentInventoryLevel--;
				showInventoryLevel(currentInventoryLevel);
			}

			else if(e.getSource().equals(item1)){
				if(lastButton != null)
					lastButton.setForeground(Color.BLACK);
				lastButton = item1;
				item1.setForeground(Color.BLUE);
				int index = currentInventoryLevel * 6;
				frame.nface.textArea.setText("Namn: "+(new Attack(listWithAttacks.get(index)).getName())+"\nTyp: "+(new Attack(listWithAttacks.get(index)).getType())+"\n"+(new Attack(listWithAttacks.get(index)).info)+"\nVälj husdjur att lägga till attacken på.");
				currentAttack=listWithAttacks.get(index);
				showPets();
			}
			else if(e.getSource().equals(item2)){
				if(lastButton != null)
					lastButton.setForeground(Color.BLACK);
				lastButton = item2;
				item2.setForeground(Color.BLUE);
				int index = currentInventoryLevel * 6+1;
				frame.nface.textArea.setText("Namn: "+(new Attack(listWithAttacks.get(index)).getName())+"\nTyp: "+(new Attack(listWithAttacks.get(index)).getType())+"\n"+(new Attack(listWithAttacks.get(index)).info)+"\nVälj husdjur att lägga till attacken på.");
				currentAttack=listWithAttacks.get(index);
				showPets();
				}
			else if(e.getSource().equals(item3)){
				if(lastButton != null)
					lastButton.setForeground(Color.BLACK);
				lastButton = item3;
				item2.setForeground(Color.BLUE);
				int index = currentInventoryLevel * 6+2;
				frame.nface.textArea.setText("Namn: "+(new Attack(listWithAttacks.get(index)).getName())+"\nTyp: "+(new Attack(listWithAttacks.get(index)).getType())+"\n"+(new Attack(listWithAttacks.get(index)).info)+"\nVälj husdjur att lägga till attacken på.");
				currentAttack=listWithAttacks.get(index);
				showPets();
				}
			else if(e.getSource().equals(item4)){
				if(lastButton != null)
					lastButton.setForeground(Color.BLACK);
				lastButton = item4;
				item2.setForeground(Color.BLUE);
				int index = currentInventoryLevel * 6+3;
				frame.nface.textArea.setText("Namn: "+(new Attack(listWithAttacks.get(index)).getName())+"\nTyp: "+(new Attack(listWithAttacks.get(index)).getType())+"\n"+(new Attack(listWithAttacks.get(index)).info)+"\nVälj husdjur att lägga till attacken på.");
				currentAttack=listWithAttacks.get(index);
				showPets();
				}
			else if(e.getSource().equals(item5)){
				if(lastButton != null)
					lastButton.setForeground(Color.BLACK);
				lastButton = item5;
				item2.setForeground(Color.BLUE);
				int index = currentInventoryLevel * 6+4;
				frame.nface.textArea.setText("Namn: "+(new Attack(listWithAttacks.get(index)).getName())+"\nTyp: "+(new Attack(listWithAttacks.get(index)).getType())+"\n"+(new Attack(listWithAttacks.get(index)).info)+"\nVälj husdjur att lägga till attacken på.");
				currentAttack=listWithAttacks.get(index);
				showPets();
				}
			else if(e.getSource().equals(item6)){
				if(lastButton != null)
					lastButton.setForeground(Color.BLACK);
				lastButton = item6;
				item2.setForeground(Color.BLUE);
				int index = currentInventoryLevel * 6+5;
				frame.nface.textArea.setText("Namn: "+(new Attack(listWithAttacks.get(index)).getName())+"\nTyp: "+(new Attack(listWithAttacks.get(index)).getType())+"\n"+(new Attack(listWithAttacks.get(index)).info)+"\nVälj husdjur att lägga till attacken på.");
				currentAttack=listWithAttacks.get(index);
				showPets();
			}else if(e.getSource().equals(activePet)){
				if(lastButton != null)
					lastButton.setForeground(Color.BLACK);
				lastButton = activePet;
				activePet.setForeground(Color.BLUE);
				//int index = currentInventoryLevel * 6 + 5;
				frame.nface.textArea.setText("Husdjur valt: "+player.activePet.name+"\nTyp:"+player.activePet.element+"\nVälj attack att ersätta.");
				showPetAttacks();
			}else if(e.getSource().equals(passivePet)){
				if(lastButton != null)
					lastButton.setForeground(Color.BLACK);
				lastButton = passivePet;
				passivePet.setForeground(Color.BLUE);
				//int index = currentInventoryLevel * 6 + 5;
				frame.nface.textArea.setText("Husdjur valt: "+player.passivePet.name+"\nTyp:"+player.passivePet.element+"\nVälj attack att ersätta.");
				showPetAttacks();
			}
			else if(e.getSource().equals(attack[0])){
				if(lastButton != null)
					lastButton.setForeground(Color.BLACK);
				if(lastButton==activePet){
					
					if(attack[0].getText().equals("Lägg till ny attack")==false && new Attack(currentAttack).type== player.activePet.element){
						player.activePet.removeAttack(player.activePet.attacks.get(0));
					}
					player.activePet.addAttack(currentAttack);
				}
				if(lastButton==passivePet){
					
					if(attack[0].getText().equals("Lägg till ny attack")==false && new Attack(currentAttack).type== player.passivePet.element){
						player.passivePet.removeAttack(player.passivePet.attacks.get(0));
					}
					player.passivePet.addAttack(currentAttack);
				}
				lastButton = attack[0];
				attack[0].setForeground(Color.BLUE);
				frame.nface.popOptions.removeAll();
				showInventoryLevel(currentInventoryLevel);
				
			}else if(e.getSource().equals(attack[1])){
				if(lastButton != null)
					lastButton.setForeground(Color.BLACK);
				if(lastButton==activePet){

					if(attack[1].getText().equals("Lägg till ny attack")==false&& new Attack(currentAttack).type== player.activePet.element){

						player.activePet.removeAttack(player.activePet.attacks.get(1));
					}
					player.activePet.addAttack(currentAttack);
				}
				if(lastButton==passivePet){
					
					if(attack[1].getText().equals("Lägg till ny attack")==false&& new Attack(currentAttack).type== player.passivePet.element){
						player.passivePet.removeAttack(player.passivePet.attacks.get(1));
					}
					player.passivePet.addAttack(currentAttack);
				}
				lastButton = attack[1];
				attack[1].setForeground(Color.BLUE);
				frame.nface.popOptions.removeAll();
				showInventoryLevel(currentInventoryLevel);
				
				
			}else if(e.getSource().equals(attack[2])){
				if(lastButton != null)
					lastButton.setForeground(Color.BLACK);
				if(lastButton==activePet){
					
					if(attack[2].getText().equals("Lägg till ny attack")==false&& new Attack(currentAttack).type== player.activePet.element){
						player.activePet.removeAttack(player.activePet.attacks.get(2));
					}
					player.activePet.addAttack(currentAttack);
				}
				if(lastButton==passivePet){
					
					if(attack[2].getText().equals("Lägg till ny attack")==false && new Attack(currentAttack).type== player.passivePet.element){
						player.passivePet.removeAttack(player.passivePet.attacks.get(2));
					}
					player.passivePet.addAttack(currentAttack);
				}
				lastButton = attack[2];
				attack[2].setForeground(Color.BLUE);
				frame.nface.popOptions.removeAll();
				showInventoryLevel(currentInventoryLevel);
				
				
			}else if(e.getSource().equals(attack[3])){
				if(lastButton != null)
					lastButton.setForeground(Color.BLACK);
				if(lastButton==activePet){
					
					if(attack[3].getText().equals("Lägg till ny attack")==false && new Attack(currentAttack).type== player.activePet.element){
						player.activePet.removeAttack(player.activePet.attacks.get(3));
					}
					player.activePet.addAttack(currentAttack);
				}
				if(lastButton==passivePet){
					
					if(attack[3].getText().equals("Lägg till ny attack")==false && new Attack(currentAttack).type== player.passivePet.element){
						player.passivePet.removeAttack(player.passivePet.attacks.get(3));
					}
					player.passivePet.addAttack(currentAttack);
				}
				lastButton = attack[3];
				attack[3].setForeground(Color.BLUE);
				frame.nface.popOptions.removeAll();
				showInventoryLevel(currentInventoryLevel);
			}else if(e.getSource().equals(back1)){
				if(lastButton != null)
					lastButton.setForeground(Color.BLACK);
				lastButton = back1;
				back1.setForeground(Color.BLUE);
				frame.nface.textArea.setText("Välj attack!");
				frame.nface.popOptions.removeAll();
				showInventoryLevel(currentInventoryLevel);
			}else if(e.getSource().equals(back2)){
				if(lastButton != null)
					lastButton.setForeground(Color.BLACK);
				lastButton = back2;
				back1.setForeground(Color.BLUE);
				frame.nface.popOptions.removeAll();
				frame.nface.textArea.setText("Namn: "+new Attack(currentAttack).getName()+"\nTyp: "+new Attack(currentAttack).getType()+"\n"+new Attack(currentAttack).info+"\nVälj husdjur att lägga till attacken på.");
				showPets();
			}
			
			frame.getContentPane().repaint();
		}
		
	}
	
	private class CustomButton extends JButton{
		
		private static final long serialVersionUID = 1L;
		

		CustomButton(){
			this.setContentAreaFilled(false);
			this.setForeground(Color.BLACK);
		}
		
		@Override
		public Dimension getMinimumSize(){
			return new Dimension(306,50);	
		}
		@Override
		public Dimension getMaximumSize(){
			return new Dimension(306,50);	
		}
		@Override
		public Dimension getPreferredSize(){
			return new Dimension(306,50);	
		}
		
	}
	
	private class CustomArrowButton extends JButton{
		
		private static final long serialVersionUID = 1L;
		CustomArrowButton(String str){

			this.setContentAreaFilled(false);
			this.setForeground(Color.BLACK);
			setText(str);
		}
		
		@Override
		public Dimension getMinimumSize(){
			return new Dimension(153,50);	
		}
		@Override
		public Dimension getMaximumSize(){
			return new Dimension(153,50);	
		}
		@Override
		public Dimension getPreferredSize(){
			return new Dimension(153,50);	
		}
		
	}
	
	private class TheMouseListener implements MouseListener{

		@Override
		public void mouseClicked(MouseEvent e){
		}

		@Override
		public void mouseEntered(MouseEvent e) {
		}

		@Override
		public void mouseExited(MouseEvent e) {
		}
		
		@Override
		public void mousePressed(MouseEvent e) {
			frame.nface.popPrime.show(frame, 50, 55);
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			
		}
		
	}
}
